$(() => {
    $("#header").load("./pages/template/header.html")
    var $nei=$(".nei")
    var $bac=$("body")
    function getPage(page) {
        $.get("/lession05/getList", { page, count: 12 }, function (data) {
           
            if (!data.error) {
                // 清空之前的数据
                $nei.empty();
                // 数据请求回来了 
                data.data.forEach((val,idx) => {
                    console.log(data.data[idx])
                    $nei.append(`
                    <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 list-item">
                        <div class="card">
                            <a href="./pages/detail.html?id=${val.goods_id}"><img src="${val.goods_small_logo}" class="card-img-top" alt="..."></a>
                        <div class="card-body">
                            <h5 class="card-title">￥${val.goods_price}</h5>
                            <p class="card-text "><a href="./pages/detail.html?id=${val.goods_id}" title="${val.goods_name}">${val.goods_name}</a></p>
                            <div class="row">
                            <div class="col-8">月销${val.add_time}  </div>
                            <a href="#" class="btn col-4 join-cart" data-id="${val.goods_id}">
                             <svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"  fill="currentColor" class="bi bi-minecart" viewBox="0 0 16 16">
                            <path d="M4 15a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm0 1a2 2 0 1 0 0-4 2 2 0 0 0 0 4zm8-1a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm0 1a2 2 0 1 0 0-4 2 2 0 0 0 0 4zM.115 3.18A.5.5 0 0 1 .5 3h15a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 14 12H2a.5.5 0 0 1-.491-.408l-1.5-8a.5.5 0 0 1 .106-.411zm.987.82 1.313 7h11.17l1.313-7H1.102z"/>
                          </svg></a>
                          </div>
                        </div>
                        </div>
                    </div>
                    `)
                })
            }
        })
    }
   ;
   var page = parseInt(Math.random()*8)
   getPage(page)
    // 请求第一页的数据

   
   
    $nei.on("click", ".join-cart", function(e) {
        console.log(this)
        // 获取当前商品id
        var id = $(this).attr("data-id");
        console.log(id)
    //    发送请求将当前商品信息id提交给服务器
        $.ajax({
            url: "/lession05/addToCart",
            data: {
                id
            },
            type: "get",
            dataType: 'json',
            success(data) {
                if (!data.error) {
                    pxmu.success(data.message)
                } else {
                    pxmu.fail(data.message)
                }
            }
        })
       

        e.preventDefault();
    })

})